from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.docs import get_redoc_html
from backend.utils.logger import logger
from fastapi.middleware.cors import CORSMiddleware
from backend.api import workbench
from backend.api import line
from backend.api import equipment
from backend.api import material_transport
from backend.api import material_pallet_type

#本地资源加载（需先禁用默认路由）
app = FastAPI(docs_url=None, redoc_url=None)

# 挂载静态目录
app.mount("/static", StaticFiles(directory="backend/static"), name="static")

# 添加 CORS 中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 自定义 Swagger UI 路由
@app.get("/docs", include_in_schema=False)
async def custom_swagger():
    return get_swagger_ui_html(
        openapi_url="/openapi.json",
        title="API Docs",
        swagger_js_url="/static/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger-ui.css"
    )

@app.get("/redoc", include_in_schema=False)
async def custom_redoc():
    return get_redoc_html(
        openapi_url="/openapi.json",
        title="API Docs (Redoc)",
        redoc_js_url="/static/redoc/redoc.standalone.js"
    )

app.include_router(workbench.router)
app.include_router(line.router)
app.include_router(equipment.router)
app.include_router(material_transport.router)
app.include_router(material_pallet_type.router)

logger.info("FastAPI app started.") 